import Paging from '../../../wxjs/Paging.js'
import getLocation from '../../../wxjs/getLocation.js'
import screen from '../../../wxjs/screen.js'
const app = getApp();

var pag = new Paging();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  drops: [{
    id: 5,
    text: '全市',
    status: true,
  }, {
    id: 4,
    text: '筛选课程',
    // status: true,
  }, {
    id: 3,
    text: '时段',
    // status: true,
  }],
  // tabId:3,
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.getLectureList,
  typeParams: {}, //获取筛选标签参数
  onLoad,
  checkDrop, //下拉选择
  selectDay, //日期选择
  completeLocation, //定位获取完成（成功，失败）
  openLocation, //查看位置
  callBack, //列表请求成功
  onShareAppMessage: function() {}
};

var page = Object.assign({}, pag.func(), getLocation, app.navToNext, screen, currentPage);

Page(page)

function onLoad(options) {
  const that = this;
  // let userId;
  let uuid = options && options.uuid || "";
  // 邀请者uuid
  let invitorUuid = options && options.invitorUuid || "";
  // 邀请活动id
  let activityUuid = options && options.activityUuid || "";
  // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  var scene = options && options.q && decodeURIComponent(options.q) || '';
  console.log("【league】options======>", options);
  console.log("【league】scene======>", scene);
  // 普通二维码进入怎么也得有10个字符
  if (scene.length > 10) {
    // 是否为json格式校验
    var tempScenes = scene.split("params=");
    if (tempScenes.length > 1) {
      tempScenes = tempScenes[1];
      var firstChar = tempScenes.charAt(0);
      var lastChar = tempScenes.charAt(tempScenes.length - 1);
      if (firstChar == "{" && lastChar == "}") {
        tempScenes = JSON.parse(tempScenes);
        // userId = tempScenes.userId;
        uuid = tempScenes.uuid;
        invitorUuid = tempScenes.invitorUuid;
        activityUuid = tempScenes.activityUuid;
      }
    }
  }

  if (invitorUuid) {
    app.invite.invitorUuid = invitorUuid;
    app.invite.activityUuid = activityUuid;
    // 异步获取用户信息
    app.xlogin.reLogin();
  }

  if (uuid) {
    wx.navigateTo({
      url: `${app.xhx_route.leagueDetails}?uuid=${uuid}`,
    })
  }
}

function checkDrop(event) {
  const that = this;
  const id = event.detail.id;

  that.typeParams.type = id || 0;
  that.setData({
    tabId: id
  })

  that.getSearchLabelByType();
}

function selectDay(event) {
  const that = this;
  const isLocation = that.data.isLocation;
  const time = event.detail.time;
  that.listParams.dateTime = time || '';

  if (isLocation) {
    // 初始化
    that.pageListInit();
  } else {
    that.getLocation();
  }
}

function completeLocation(res) {
  const that = this;
  let latitude = res.latitude || '';
  let longitude = res.longitude || '';

  if (!that.listParams.latitude) that.listParams.latitude = latitude;
  if (!that.listParams.longitude) that.listParams.longitude = longitude;
  if (!that.listParams.areas) that.listParams.areas = [];
  if (!that.listParams.gymTypeIds) that.listParams.gymTypeIds = [];
  if (!that.listParams.intervalTimes) that.listParams.intervalTimes = [];
  if (!that.listParams.tGoodsLabelNameUuids) that.listParams.tGoodsLabelNameUuids = [];
  if (!that.listParams.tGymUuids) that.listParams.tGymUuids = [];
  if (!that.listParams.isShadowOver) that.listParams.isShadowOver = 0;

  that.typeParams.latitude = latitude;
  that.typeParams.longitude = longitude;
  // 初始化
  that.pageListInit();

  that.setData({
    latitude,
    longitude
  })
}

function openLocation(event) {
  const that = this;
  const latitude = event.currentTarget.dataset.latitude;
  const longitude = event.currentTarget.dataset.longitude;
  app.middleware.openLocation({
    latitude,
    longitude,
  });
}

function callBack(res, lists) {
  const that = this;
  var page = that.listParams.page;
  var row = that.listParams.row;
  var isReach = that.listParams.isReach;
  var footMsg = "";
  var tempLists = res.data || [];
  // 当前请求的课程条数
  let length = 0;

  for (let i = 0; i < tempLists.length; i++) {
    length += tempLists[i].lectureEntities.length;
    // 当前健身房是否已经存在
    let isGym = false;
    for (let j = 0; j < lists.length; j++) {
      if (tempLists[i].uuid == lists[j].uuid) {
        isGym = true;
        let lectureEntities = lists[j].lectureEntities || [];
        lists[j].lectureEntities = lectureEntities.concat(tempLists[i].lectureEntities);
      }
    }
    // 当前健身房不存在。则新增一个健身房数据
    if (!isGym) {
      lists.push(tempLists[i]);
    }
  }

  if (row <= length) {
    isReach = false;
    footMsg = "上拉加载更多";
  } else {
    isReach = true;
    footMsg = "没有更多数据啦！";
  }
  that.setData({
    msg: "",
    lists,
    footMsg: footMsg,
  })
  that.listParams.page = page;
  that.listParams.isReach = isReach;
}